<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-alert
        :closable="false"
        show-icon
        title="操作提示：按小区导出车辆列表"
        type="info"
      />
      <el-form-item label="小区名称" prop="areaNo">
        <el-select
          v-model="form.areaNo"
          class="full-select"
          clearable
          placeholder="请选择小区名称"
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="item in cellCode"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button :loading="isSaving" type="primary" @click="save">
        导 出
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { exportCarList } from '@/api/record'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ExportCarList',
    data() {
      return {
        form: {
          areaNo: '',
        },
        rules: {
          areaNo: [
            { required: true, trigger: 'change', message: '请选择小区名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        isSaving: false,
      }
    },
    computed: {
      ...mapGetters({
        cellCode: 'dict/cellCode',
      }),
    },
    created() {},
    methods: {
      showEdit() {
        this.title = '按小区导出车辆信息'
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.isSaving = true
            exportCarList(this.form.areaNo)
              .then((res) => {
                if (res && res.status === 0) {
                  this.$baseMessage(
                    '数据导出成功！',
                    'success',
                    'vab-hey-message-success'
                  )
                  this.close()
                } else {
                  this.$baseMessage(
                    res ? res.msg : '数据导出失败！',
                    'error',
                    'vab-hey-message-error'
                  )
                }
              })
              .catch((err) => {
                console.log(err)
                this.$baseMessage(
                  '导出失败，请重试！',
                  'error',
                  'vab-hey-message-error'
                )
              })
              .then(() => {
                this.isSaving = false
              })
          }
        })
      },
    },
  }
</script>
